package com.matt.service.impl;

import com.matt.commons.page.PagerQuery;
import com.matt.domain.ChildrenMemorandum;
import com.matt.domain.ChildrenVaccine;
import com.matt.domain.vo.ChildrenMemorandumVo;
import com.matt.repository.ChildrenMemorandumRepository;
import com.matt.service.ChildrenMemorandumService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2017/7/13
 * *******************************
 */
@Service
@Slf4j
public class ChildrenMemorandumServiceImpl implements ChildrenMemorandumService {

    @Autowired
    private ChildrenMemorandumRepository childrenMemorandumRepository;


    @Override
    public void addChildrenMemorandum(ChildrenMemorandum childrenMemorandum) {
        childrenMemorandumRepository.save(childrenMemorandum);
    }

    @Override
    public void delChildrenMemorandum(String id) {
        childrenMemorandumRepository.deleteById(id);
    }

    @Override
    public void delChildrenMemorandumByList(List<ChildrenMemorandum> childrenMemorandumList) {
        childrenMemorandumRepository.deleteAll(childrenMemorandumList);
    }


    @Override
    public ChildrenMemorandum selChildrenMemorandumById(String id) {
        ChildrenMemorandum childrenMemorandum = childrenMemorandumRepository.findById(id).get();
        return childrenMemorandum;
    }

    @Override
    public List<ChildrenMemorandum> selChildrenMemorandumList(String id) {
        // 使用 Specification 构建动态查询条件
        Specification<ChildrenMemorandum> specification = (root, query, criteriaBuilder) -> {
            // 创建一个空的 Predicate 列表，用于存储查询条件
            List<Predicate> predicates = new ArrayList<>();

            // 判断 classesId 是否为空，不为空则添加到查询条件中
            if (id != null && !id.isEmpty()) {
                predicates.add(criteriaBuilder.equal(root.get("relChildrenMemorandumId"), id));
            }

            // 将多个条件用 AND 连接
            return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
        };
        List<ChildrenMemorandum> all = childrenMemorandumRepository.findAll(specification);
        return all;
    }

}
